/**
 * 用数组模拟实现Queue
 * 采取浪费一个空间来区别满和空
 */
public class MySqQueue {
    private int[] elem;
    private int head;
    private int tail;

    public MySqQueue() {
        this.elem = new int[5];
    }

    //入队
    public boolean offer(int data) {
        if (full()) {
            System.out.println("队列已满");
            return false;
        }
        elem[tail] = data;
        tail = (tail + 1) % elem.length;
        return true;
    }

    //出队
    public int poll() {
        if (empty()) {
            throw new EmptyExeception("队列空！");
        }
        int res = elem[head];
        head = (head + 1) % elem.length;
        return res;
    }

    //获取队首元素
    public int head() {
        if (empty()) {
            throw  new EmptyExeception("队列空！");
        }
        return elem[head];
    }

    //获取队尾元素
    public int rear() {
        if (empty()) {
            throw  new EmptyExeception("队列空！");
        }
        int index = tail == 0 ? elem.length - 1 : tail - 1;
        return elem[index];
    }

    //判满
    public boolean full() {
        return ((tail + 1) % elem.length == head);
    }

    //判空
    public boolean empty() {
        return head == tail;
    }
}
